Import grass

Import required libraries and set up some variables


In [1]:
import grass.script.setup as gsetup
import grass.script as grass
import subprocess
import os
import sys
rundir   = 'grassdata'
location = 'location'

Set name of mapset


In [2]:
mapset   = 'MyMapset'

Create mapset


In [23]:
gisbase  = os.environ['GISBASE'] = '/usr/lib/grass74'
gisdbase = os.path.join(os.environ['HOME'], rundir)
sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))
gsetup.init(gisbase,gisdbase, location, 'PERMANENT')
a=grass.start_command('g.mapset', flags='c',stderr=subprocess.PIPE,mapset=mapset,location=location)
stdoutdata, stderrdata = a.communicate()
gsetup.init(gisbase,gisdbase, location, mapset)


Out[23]:
'/tmp/tmprYpWjR'

Set up import parameters

Uncommented parameters are required. Uncomment other to add options.


In [19]:
kwargs={'input':'/eodata/Landsat-5/TM/L1G/1984/04/06/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F.TIFF/LT52070181984097ESA00_B1.TIF',
'output':'image.tif',
# 'band':'integer[,integer,...]',
 'memory':2047,
# 'target':'name',
# 'title':'phrase',
# 'offset':'integer',
# 'location':'name',
# 'table':'file'
}

Proceed with import


In [24]:
a=grass.start_command('r.import',stderr=subprocess.PIPE, **kwargs)

In [25]:
stdoutdata, stderrdata = a.communicate()

In [26]:
print stdoutdata,stderrdata


None ERROR 4: /eodata/Landsat-5/TM/L1G/1984/04/06/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F.TIFF/LT52070181984097ESA00_B1.TIF: Invalid argument
ERROR: Unable to open datasource
       </eodata/Landsat-5/TM/L1G/1984/04/06/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F.TIFF/LT52070181984097ESA00_B1.TIF>
ERROR 4: /eodata/Landsat-5/TM/L1G/1984/04/06/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F.TIFF/LT52070181984097ESA00_B1.TIF: Invalid argument
ERROR: Unable to open datasource
       </eodata/Landsat-5/TM/L1G/1984/04/06/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F.TIFF/LT52070181984097ESA00_B1.TIF>
ERROR: Unable to read GDAL dataset
       </eodata/Landsat-5/TM/L1G/1984/04/06/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F/LS05_RFUI_TM__GEO_1P_19840406T105359_19840406T105428_000524_0207_0018_654F.TIFF/LT52070181984097ESA00_B1.TIF>